/*
 * TowerBlueOne.java
 *
 * Created on 27 December 2007, 12:37
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package hextd.tower;

import hextd.hex.Hex;
import hextd.sprite.EnemySprite;
import hextd.util.Context;
import hextd.util.Geometry;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Area;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;

/**
 *
 * @author hylke
 */
public class TowerBlueOne extends TowerBase {
    
    public static int price = 500;
    public static int damage = 0;
    public static float range = 5f;
    
    private int fireAt = -1;
    private float enemyX = 0;
    private float enemyY = 0;
    private EnemySprite.COLOR damageColor = EnemySprite.COLOR.BLUE;
    private int coolDown = 0;
    private int coolDownMax = 79;
    private String[] newModes = {"Fastest"};
    
    private Color transColor;
    
    private EnemySprite[] currentTargets = new EnemySprite[0];
    
    /** Creates a new instance of TowerBlueOne */
    public TowerBlueOne(Context context, Hex hex) {
        super(TowerFactory.type.blueOne, price, damage, range);
        this.lineColor = new Color(75, 75, 255);
        this.transColor = new Color(this.lineColor.getRed(), this.lineColor.getGreen(), this.lineColor.getBlue(), 75);
        this.modes = this.newModes;
        this.doInit(context, hex);
    }
    
    public String getStatusString() {
        String retString = "";
        retString += "<h1>Blue Tower 1</h1>" +
                "<p>" +
                "<b>Level:</b> "+(1+this.level)+"<br>\n" +
                "<b>Slowdown:</b> "+TowerBlueOne.form.format(100f-100f/(2+this.level))+"%<br>\n" +
                "<b>Range:</b> "+this.rangeText+"<br>" +
                "</p>\n";
        return retString;
    }
    
    public String getInfoString() {
        String retString = "";
        retString += "<h1>Blue Tower 1</h1>" +
                "<p>" +
                "Tower with four energy beams that overheat the targets engines, reducing it's top speed.<br>" +
                "More hits make the effect last longer and upgrading the tower makes the effect stronger." +
                "<br>It's effect is strong against blue enemies but very weak against yellow enemies." +
                "</p>" +
                "<p>" +
                "<b>Price:</b> "+TowerBlueOne.price+"<br>" +
                "<b>Damage:</b> none, slowdown instead<br>" +
                "<b>Range:</b> "+TowerBlueOne.form.format(rangeCurrent)+"<br>" +
                "<b>Refire rate:</b> 0.25/s"+
                "</p>";
        return retString;
    }
    
    public void doTick(int gameTime) {
        if (this.coolDown > 0) {
            this.coolDown--;
        } else {
            this.currentTargets = this.findEnemiesFastest(this.xFloat, this.yFloat, this.rangeCurrent, 4, false);
            if (this.currentTargets.length > 0) {
                EnemySprite enemy;
                for (int i=0; i<this.currentTargets.length; i++) {
                    enemy = this.currentTargets[i];
                    enemy.doDamage(1f/(2f+this.level), EnemySprite.DamageType.power, this.damageColor);
                }
                
                //System.out.println("TR! fire at tick: "+gameTime);
                this.coolDown = this.coolDownMax;
            }
        }
    }
    
    protected void generateShapes() {
        
        int shapeCount = 3;
        int i;
        float[][] points = Geometry.createPolyCoords(this.paintScale);
        float[][] points2 = Geometry.createPolyCoords(this.paintScale*0.2f);
        
        this.shapes = new Shape[shapeCount];
        i=0;
        GeneralPath poly = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
        poly.append(new CubicCurve2D.Float(points[0][0], points[0][1], points2[0][0], points2[0][1], points2[2][0], points2[2][1], points[2][0], points[2][1]), true);
        poly.append(new CubicCurve2D.Float(points[2][0], points[2][1], points2[2][0], points2[2][1], points2[4][0], points2[4][1], points[4][0], points[4][1]), true);
        poly.append(new CubicCurve2D.Float(points[4][0], points[4][1], points2[4][0], points2[4][1], points2[0][0], points2[0][1], points[0][0], points[0][1]), true);
        poly.append(new CubicCurve2D.Float(points[1][0], points[1][1], points2[1][0], points2[1][1], points2[3][0], points2[3][1], points[3][0], points[3][1]), false);
        poly.append(new CubicCurve2D.Float(points[3][0], points[3][1], points2[3][0], points2[3][1], points2[5][0], points2[5][1], points[5][0], points[5][1]), true);
        poly.append(new CubicCurve2D.Float(points[5][0], points[5][1], points2[5][0], points2[5][1], points2[1][0], points2[1][1], points[1][0], points[1][1]), true);
        poly.append(new Ellipse2D.Float(-0.6f*this.paintScale, -0.6f*this.paintScale, 1.2f*this.paintScale, 1.2f*this.paintScale), false);
        this.shapes[i++] = new Area(poly);
        this.shapes[i++] = this.shapes[0];
        this.shapes[i++] = new Ellipse2D.Float(-1.0f*this.context.strokeScale, -1.0f*this.context.strokeScale, 2.0f*this.context.strokeScale, 2.0f*this.context.strokeScale);
        
        i=0;
        this.shapeAction = new int[shapeCount];
        this.shapeAction[i++] = 2;
        this.shapeAction[i++] = 1;
        this.shapeAction[i++] = 1;
        
        i=0;
        this.shapeColor = new Color[shapeCount];
        this.shapeColor[i++] = this.transColor;
        this.shapeColor[i++] = this.lineColor;
        this.shapeColor[i++] = this.lineColor;
        
        i=0;
        this.strokes = new Stroke[shapeCount];
        this.strokes[i++] = new BasicStroke(2.0f*this.context.strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
        this.strokes[i++] = this.strokes[0];
        this.strokes[i++] = this.strokes[0];
    }
    
    public void paintEffects(Graphics2D g2, int gameTime) {
        Stroke defaultStroke = g2.getStroke();
        
        if (this.currentTargets.length > 0 && this.coolDown > this.coolDownMax-3) {
            g2.setColor(this.lineColor);
            for (int i=0; i<this.currentTargets.length; i++) {
                g2.draw(new Line2D.Float( this.xPaint, this.yPaint, currentTargets[i].getPaintX(), currentTargets[i].getPaintY() ));
            }
        }
        
        g2.setStroke(defaultStroke);
    }
    
}
